<template>
  <div class="app-container">
    <el-table :data="banners" style="width: 100%" border>
      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column label="标题" width="100" align="center">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>

      <el-table-column label="描述" align="center">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>

      <el-table-column label="中图预览" align="center">
        <template slot-scope="scope">
          <el-image
            style="width:180px;"
            :src="scope.row.midImg2"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="大图预览" align="center">
        <template slot-scope="scope">
          <el-image
            style="width:180px;"
            :src="scope.row.bigImg2"
            fit="cover"
          ></el-image>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="60" align="center">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="编辑"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              @click="openEditDialog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="编辑标语" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>

        <el-form-item label="描述">
          <el-input type="textarea" :rows="3" v-model="form.description"></el-input>
        </el-form-item>
      
        <el-row>
          <el-col :span="12">
            <el-form-item label="中图">
              <Upload :src="form.midImg"  @upload-success="form.midImg = $event"></Upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="大图">
              <Upload :src="form.bigImg" @upload-success="form.bigImg = $event"></Upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBannerConfirm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getBanner, setBanner } from "@/api/banner";
import { SERVER_URL } from "@/urlConfig";
import Upload from "@/components/Upload";

export default {
  data() {
    return {
      banners: [],
      dialogFormVisible: false,
      form: {
        id: "",
        midImg: "",
        bigImg: "",
        title: "",
        description: "",
      },
    };
  },
  components: {
    Upload
  },
  created() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      var res = await getBanner();
      this.banners = res.data.map((item) => {
        return {
          ...item,
          midImg2: SERVER_URL + item.midImg,
          bigImg2: SERVER_URL + item.bigImg,
        };
      });
    },
    openEditDialog(bannerItem) {
      this.form = {
        ...bannerItem
      };
      this.dialogFormVisible = true;
    },
    async editBannerConfirm(){
      let newBanners = [...this.banners];
      for(var i = 0; i < newBanners.length; i++) {
        if(newBanners[i].id === this.form.id){
          newBanners[i] = this.form;
        }
      }
      await setBanner(newBanners);
      this.dialogFormVisible = false;
      this.$message({
        message: "操作成功",
        type: "success"
      });
      this.fetchData();
    }
  },
};
</script>

<style scoped lang="scss">
</style>
